package com.vf.cloud.paas.app.master.api.asset.controller;

import java.util.List;
import org.bson.types.Binary;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import com.jfinal.kit.StrKit;
import com.vf.cloud.paas.app.master.api.file.service.IModelFile;
import com.vf.cloud.paas.app.master.api.file.service.IThumbnail;
import com.vf.cloud.paas.app.master.common.domain.Asset;
import com.vf.cloud.paas.app.master.common.repository.base.ModelFile;
import com.vf.cloud.paas.app.master.common.repository.base.Thumbnail;
import com.vf.cloud.paas.common.datasource.annotation.Tx;
import com.vf.cloud.paas.common.util.R;
import com.vf.cloud.paas.common.util.UuidUtil;
import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
@RestController
@RequestMapping("/api/v1/asset")
public class AssetController {

	@Autowired
	private IThumbnail thumbnailImpl;

	@Autowired
	private IModelFile modelFileImpl;

	@RequestMapping(value = "/list", method = RequestMethod.POST)
	public R<List<Asset>> list(@RequestBody Asset param) {
		String sqlExceptSelect = " from " + Asset.TABLE_NAME + "  a WHERE a.classify_id='" + param.getClassifyId()
				+ "' ";
		sqlExceptSelect += "  ORDER BY a.name  , a.modify_time desc ";
		if (!StrKit.isBlank(param.getName())) {
			sqlExceptSelect += " and name like '%" + param.getName() + "%' ";
		}
		return R.ok(Asset.dao.find("select *  " + sqlExceptSelect));
	}

	@Tx
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public R<Asset> save(MultipartFile thumbnailFile, MultipartFile modelFile, Asset asset) {
		try {
			Thumbnail thumbnailEntity = null;
			if (thumbnailFile != null) {
				thumbnailEntity = new Thumbnail();
				thumbnailEntity.setSize(thumbnailFile.getSize());
				thumbnailEntity.setName(asset.getName());
				thumbnailEntity.setContent(new Binary(thumbnailFile.getBytes()));
			}

			ModelFile modelFileEntity = null;
			if (modelFile != null) {
				modelFileEntity = new ModelFile();
				modelFileEntity.setSize(modelFile.getSize());
				modelFileEntity.setName(asset.getName());
				modelFileEntity.setContent(new Binary(modelFile.getBytes()));
			}

			if (StrKit.notBlank(asset.getId())) {
				if (asset.update()) {
					if (thumbnailEntity != null) {
						try {
							thumbnailEntity.setId(asset.getId());
							thumbnailImpl.save(thumbnailEntity);
						} catch (Exception e) {
						}
					}

					if (modelFileEntity != null) {
						try {
							modelFileEntity.setId(asset.getId());
							modelFileImpl.save(modelFileEntity);
						} catch (Exception e) {
						}
					}

					return R.ok(asset);
				} else {
					return R.failed("更新失败！");
				}
			} else {
				asset.setId(UuidUtil.getUUID());
				if (asset.save()) {
					if (thumbnailEntity != null) {
						try {
							thumbnailEntity.setId(asset.getId());
							thumbnailImpl.save(thumbnailEntity);
						} catch (Exception e) {
						}
					}

					if (modelFileEntity != null) {
						try {
							modelFileEntity.setId(asset.getId());
							modelFileImpl.save(modelFileEntity);
						} catch (Exception e) {
						}
					}
					return R.ok(asset);
				} else {
					return R.failed("保存失败！");
				}
			}
		} catch (Exception e) {
			return R.failed(e.getMessage());
		}
	}

	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	public R<String> delete(@RequestBody Asset param) {
		if (param == null) {
			return R.failed("参数必填.");
		}
		if (Asset.dao.deleteById(param.getId())) {
			thumbnailImpl.deleteById(param.getId());
			modelFileImpl.deleteById(param.getId());
			return R.ok();
		}
		return R.failed();
	}

}
